package com.csh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csh.user.entity.TbAdmin;
import com.csh.user.entity.dto.AdminEntity;
import com.csh.user.service.TbAdminService;
import com.csh.user.mapper.TbAdminMapper;
import com.csh.user.utils.Note;
import com.csh.utils.ResultResponse;
import com.csh.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author 86176
* @description 针对表【tb_admin】的数据库操作Service实现
* @createDate 2022-08-25 20:11:38
*/
@Service
public class TbAdminServiceImpl extends ServiceImpl<TbAdminMapper, TbAdmin>
    implements TbAdminService{
    @Autowired
    TbAdminMapper tbAdminMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public ResultResponse login(AdminEntity adminEntity) {
        String name = adminEntity.getName();
        List<TbAdmin> tbAdmins = tbAdminMapper.selectList(
                new QueryWrapper<TbAdmin>().lambda().eq(TbAdmin::getName, name)
        );
        if(tbAdmins.size()>0){
            TbAdmin tbAdmin = tbAdmins.get(0);
            if(tbAdmin.getPassword().equals(adminEntity.getPassword())){
                String token = TokenUtils.token().setKey("123456")
                        .setExpire(60 * 30)
                        .setKey("id" + tbAdmin.getId())
                        .setKey("userName" + tbAdmin.getName())
                        .makeToken();
                //把token保存到redis中
                redisTemplate.opsForValue().set(token,token,30, TimeUnit.MINUTES);
                tbAdmin.setToken(token);
                return ResultResponse.SUCCESS(tbAdmin);

            }
            return ResultResponse.FAILED(404,"输入的密码错误");
        }
        return ResultResponse.FAILED(404,"输入的账号错误");

    }

    @Override
    public ResultResponse logins(AdminEntity adminEntity) {
        String phone = adminEntity.getPhone();
        List<TbAdmin> tbAdmins = tbAdminMapper.selectList(
                new QueryWrapper<TbAdmin>().lambda().eq(TbAdmin::getPhone, phone)
        );
        if(tbAdmins.size()>0){
            TbAdmin tbAdmin = tbAdmins.get(0);
            String send = Note.Send(tbAdmin.getPhone());
            System.out.println("验证码成功:"+send);
            //把验证码保存到redis中
            redisTemplate.opsForValue().set(adminEntity.getPhone(),send,1,TimeUnit.MINUTES);
            return ResultResponse.SUCCESS();
        }
        return ResultResponse.FAILED(404,"输入的手机号错误,没有这个手机号");

    }

    @Override
    public ResultResponse dxlogin(AdminEntity adminEntity) {
        String phone = adminEntity.getPhone();
        List<TbAdmin> tbAdmins = tbAdminMapper.selectList(
                new QueryWrapper<TbAdmin>().lambda().eq(TbAdmin::getPhone, phone)
        );
        if(tbAdmins.size()>0){
            TbAdmin tbAdmin = tbAdmins.get(0);
            String  send = (String) redisTemplate.opsForValue().get(adminEntity.getPhone());
            if(send.equals(adminEntity.getYzm())){
                String token = TokenUtils.token().setKey("123456")
                        .setExpire(60 * 30)
                        .setKey("id" + tbAdmin.getId())
                        .setKey("userName" + tbAdmin.getName())
                        .makeToken();
                //把token保存到redis中
                redisTemplate.opsForValue().set(token,token,30, TimeUnit.MINUTES);
                tbAdmin.setToken(token);
                return ResultResponse.SUCCESS(tbAdmin);
            }
        }


        return ResultResponse.FAILED(404,"验证码错误");
    }
}




